/*****************************************************************************
 * plum_hal_spi.h
 *
 * Copyright (C) 2020 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#ifndef __PLUM_HAL_SPI_H__
#define __PLUM_HAL_SPI_H__
/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Public Types
 ****************************************************************************/

typedef enum {
    PLUM_HAL_SPI_MODE_0,
    PLUM_HAL_SPI_MODE_1,
    PLUM_HAL_SPI_MODE_2,
    PLUM_HAL_SPI_MODE_3,
} plum_hal_spi_mode_t;

typedef struct {
    plum_u32 mosi;
    plum_u32 miso;
    plum_u32 clk;
} plum_hal_spi_gpio_t;

typedef struct {
    plum_hal_spi_gpio_t gpio;

    plum_hal_spi_mode_t mode;
    plum_u32            freq;
    plum_u8             word_width;
} plum_hal_spi_cof_t;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/**
 * @brief:  SPI初始化
 *
 * @param[in]:  id      -   SPI端口号
 * @param[in]:  config  -   端口配置指针
 **/
plum_s32 plum_hal_spi_init(plum_u32 id, plum_hal_spi_cof_t *config);

/**
 * @brief:  SPI端口交换数据
 *
 * @param[in]:  id     -   SPI端口号
 * @param[in]:  rData  -   端口接收数据指针
 * @param[out]：wData  -    端口发送数据指针
 * @param[in]:  len    -   端口读写数据长度
 *
 * @return: 成功  -   返回0
 *          失败  -   返回错误码
 *
 * @note:   spi只负责数据传输，片选一律由外部控制
 **/
plum_s32 plum_hal_spi_transfer(plum_u32 id, plum_u8 *rData, plum_u8 *wData,
                               plum_u32 len);

/**
 * @brief:  通过SPI总线接收一个字节数据
 *
 * @param[in]:  id      -   SPI ID
 * @param[out]: rData   -   数据输入指针
 *
 * @return: 返回执行状态
 */
plum_s32 plum_hal_spi_recv_byte(plum_u32 id, plum_u8 *rData);

/**
 * @brief:  通过SPI总线发送一个字节数据
 *
 * @param[in]:  id      -   SPI ID
 * @param[in]:  wData   -   待写入数据
 *
 * @return: 返回执行状态
 */
plum_s32 plum_hal_spi_send_byte(plum_u32 id, plum_u8 wData);

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_HAL_SPI_H__ */
/****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
